//
//  QSSignInRecordHeaderView.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/20.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSSignInRecordHeaderView.h"
#import "QSSingInInterface.h"
 
@implementation QSSignInRecordHeaderView
#pragma mark - **************** Initials
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor QSColorWhiteFFFFFF];
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
          _userModel = [QSUserManager sharedQSUserManager].user;
        [self getApiSignDetail];//获取人员/组员列表
        [self configSubView];
    }
    return self;
}

- (void)configSubView{
    [self addSubview:self.nameLabel];
    
//    [self addSubview:self.selectPeopleButton];
//    [self addSubview:self.selectMenu];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(kRealValue(16));
        make.top.equalTo(self).offset(kRealValue(15));
    }];


    NSString *name;
    if (kCurrentUser.enterpriseLevel.integerValue == 1) {//服务人员 //epType为1 驻企服务员 epType为2 乡镇联企干部  epType为3 区联企领导
        if (kCurrentUser.epType.integerValue == 1) {
            name = @"驻企服务员";
        }
        if (kCurrentUser.epType.integerValue == 2) {
            name = @"乡镇联企干部";
        }
        if (kCurrentUser.epType.integerValue == 3) {
            name = @"区联企领导";
        }
    }else{
        if (kCurrentUser.enterpriseLevel.integerValue == 2) {
            name = @"组长";
        }else{
            name = @"领导";
        }
    }
    NSInteger lenth = kCurrentUser.name.length;
   
    _nameLabel.text = [NSString stringWithFormat:@"尊敬的 %@ 您好！", kCurrentUser.name];
    [_nameLabel setAttributedText:[UILabel setLabelTextColorWithString:_nameLabel.text Range:NSMakeRange(4, lenth) andColor:[UIColor colorWithHexString:@"#128BEC"]]];
}


- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel labelWithName:@"" font:[UIFont systemFontOfSize:15 weight:1] textColor:[UIColor QSColorBlack434343] textAlignment:NSTextAlignmentLeft];
    }
    return _nameLabel;
}



#pragma mark - Action
- (void)loadFinishWithEpID:(NSString *)epid
                      EgID:(NSString *)egid{
    if (self.recordHeaderViewDidFinishLoadOpenList) {
        self.recordHeaderViewDidFinishLoadOpenList(self.openListArr, egid, epid);
    }
}

#pragma mark - Request
- (void)getApiSignDetail{
    NSString *key;
    if (_userModel.enterpriseLevel.integerValue == 1) {
        key = @"companyList";
    }else if (_userModel.enterpriseLevel.integerValue == 2){
        key = @"personList";
    }else{
        key = @"groupList";
    }
    NSLog(@"当前身份%@", _userModel.enterpriseLevel);
    [QSSingInInterface getApiSignDetailWithLevel:[_userModel.enterpriseLevel integerValue] dBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, NSDictionary * _Nonnull result) {
        if (rspStatusAndMessage.code == kResponseSuccessCode) {
            NSLog(@"%@", result);
            NSString *epId = result[@"epId"];//组长取egid
            NSString *egId = result[@"egId"];//领导取组
            if (kCurrentUser.enterpriseLevel.integerValue == 2) {
                self.roleName = result[@"egName"];
//                [self updateUi];
            }
            self.openListArr = (NSArray *)result[key];
//            if (self.openListArr.count) {
                [self loadFinishWithEpID:epId EgID:egId];
//            }
        }
    }];
}


- (void)updateUi{
    NSString *groupNameText = [self.roleName stringByAppendingString:@"组长"];
    NSInteger lenth = groupNameText.length;
    _nameLabel.text = [NSString stringWithFormat:@"尊敬的 %@ 您好！", groupNameText];
    [_nameLabel setAttributedText:[UILabel setLabelTextColorWithString:_nameLabel.text Range:NSMakeRange(4, lenth) andColor:[UIColor colorWithHexString:@"#128BEC"]]];
    [self bringSubviewToFront:self.nameLabel];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rectb{
    // Drawing code
}
*/

@end
